﻿using Books.Common;
using Books.ViewModel;
using Microsoft.Practices.ServiceLocation;
using Services.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Books.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainBook : Page
    {
        public MainBook()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Handle events from system transport contrtols when playing background-capable media

        }


        private ISeriesViewModel seriesViewModel
        {
            get { return ServiceLocator.Current.GetInstance<ISeriesViewModel>(); }
        }
        private void GenresClick(object sender, ItemClickEventArgs e)
        {
            var getData = e.ClickedItem as GenreList;
            seriesViewModel.GetSeries(getData);
            Frame.Navigate(typeof(DetailGenres));


        }
        private IBookDetailViewModel bookDetailViewModel
        {
            get { return ServiceLocator.Current.GetInstance<IBookDetailViewModel>(); }
        }
        private async void Newest_ItemClick(object sender, ItemClickEventArgs e)
        {
            var data = e.ClickedItem as BookList;
            //HandleInAppPurchase purchase = new HandleInAppPurchase();
//#if DEBUG
//            var isPurchaseSuccess = await purchase.LoadInAppPurchaseProxyFileAsync(data.book_id);
//#else
//            var isPurchaseSuccess = await purchase.LoadInAppPurchaseAsync(data.book_id);
//#endif
//            if (isPurchaseSuccess)
//            {
                
                bookDetailViewModel.SetDataBookDetail(data.book_id, GetDeviceId.GetHardwareId());
                Frame.Navigate(typeof(DetailPage));
            //}
        }

        private void keySearch(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (!string.IsNullOrEmpty(TxtSearchBooks.QueryText))
                {
                    SendSearch();
                }
            }
        }

        private void SendSearch()
        {
            App.GenreSearch = (bool)SearchType.IsChecked;
            App.StringSearch = TxtSearchBooks.QueryText;
            Frame.Navigate(typeof(SearchBookPage));
        }

        private void TxtSearchBooks_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
        {
            if (!string.IsNullOrEmpty(TxtSearchBooks.QueryText))
            {
                SendSearch();
            }
        }


        private void AllBooks_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.PageTypeNavigate = ViewModel.Paging.PageType.AllBooks;
            App.rootFrame.Navigate(typeof(BookCategories));
        }

        private void HotBooks_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.PageTypeNavigate = ViewModel.Paging.PageType.HotBooks;
            App.rootFrame.Navigate(typeof(BookCategories));
        }

        private void NewestBooks_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.PageTypeNavigate = ViewModel.Paging.PageType.WestBooks;
            App.rootFrame.Navigate(typeof(BookCategories));
        }

        private void RecommendBooks_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.PageTypeNavigate = ViewModel.Paging.PageType.RecommendBooks;
            App.rootFrame.Navigate(typeof(BookCategories));
        }

        private void SeriesBooks_Tapped(object sender, TappedRoutedEventArgs e)
        {

        }

    }
}
